package com.iflysse.controller;


import com.iflysse.common.ResultEnum;
import com.iflysse.pojo.bo.CommonBoResp;
import com.iflysse.pojo.bo.req.*;
import com.iflysse.pojo.bo.resp.ResourceBoResp;
import com.iflysse.pojo.bo.resp.ResourceListBoResp;
import com.iflysse.pojo.vo.common.CommonVoResp;
import com.iflysse.pojo.vo.req.*;
import com.iflysse.pojo.vo.resp.AddResourceRichImgResp;
import com.iflysse.pojo.vo.resp.AddResourceRichImgVoResp;
import com.iflysse.pojo.vo.resp.ResourceResp;
import com.iflysse.pojo.vo.resp.ResourceVoResp;
import com.iflysse.services.ResourceService;
import com.iflysse.util.CommonUtil;
import com.iflysse.util.FileUpdateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javafx.scene.chart.ValueAxis;
import lombok.extern.log4j.Log4j;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/resource")
@Api(value = "老师端资源的接口")
@Log4j
public class ResourceController  {



    @Autowired
   private ResourceService resourceService;


    @PostMapping(value = "/uploadRickImg")
    @ApiOperation(value = "富文本框 图片上传接口")
    public AddResourceRichImgVoResp doUploadRickImg(@RequestParam("richImg") MultipartFile file, AddResourceRichImgVoReq addResourceRichImgVoReq) {

        AddResourceRichImgVoResp addResourceRichImgVoResp=new AddResourceRichImgVoResp();
        try {

            log.info("老师端富文本框图片上传参数bean:"+addResourceRichImgVoReq);
            String path=FileUpdateUtil.fileUploadToBoot(file,FileUpdateUtil.rickImgSuffix);

            AddResourceRichImgResp addResourceRichImgResp=new AddResourceRichImgResp();
            addResourceRichImgResp.setUrl(FileUpdateUtil.prefixResources+path);
            addResourceRichImgVoResp.setResult(addResourceRichImgResp);
            addResourceRichImgVoResp.setCode(ResultEnum.SUCCESS.getCode());
            addResourceRichImgVoResp.setMsg(ResultEnum.SUCCESS.getMsg());

            log.info("老师端上传视频资源响应bean:"+addResourceRichImgVoResp);
            return addResourceRichImgVoResp;
        }catch (Exception e){
            log.error("教师端添加资源出现异常，信息为："+e);
            CommonUtil.printError(addResourceRichImgVoResp);
            return addResourceRichImgVoResp;
        }
    }


    @PostMapping("/addResourceHtml")
    @ApiOperation(value ="添加HTML 页面资源" )
    public CommonVoResp doAddResourceHtml(AddResourceHtmlVoReq addResourceHtmlVoReq){

        CommonVoResp commonVoResp=new CommonVoResp();

        try {
            log.info("教师端添加html课程资源参数："+addResourceHtmlVoReq);
            AddResourceHtmlBoReq addResourceHtmlBoReq= CommonUtil.objToObj(addResourceHtmlVoReq,AddResourceHtmlBoReq.class);
            addResourceHtmlBoReq.setChapterId(addResourceHtmlVoReq.getChapterId());
            addResourceHtmlBoReq.setResourceTitle(addResourceHtmlVoReq.getResourceTitle());
            addResourceHtmlBoReq.setKnowledgePointIdList(Arrays.asList(addResourceHtmlVoReq.getKnowledgePointIdList().split(",")));
            log.info("教师端添加html课程资源调用业务参数："+addResourceHtmlBoReq);
            CommonBoResp commonBoResp= resourceService.addResource(addResourceHtmlBoReq);
            CommonUtil.printBoResultToVoResult(commonBoResp,commonVoResp);
            log.info("教师端添加html课程资源结果："+commonVoResp);
            return commonVoResp;
        }catch (Exception e){
            log.info("添加课程页面资源出现异常，信息为："+e);
            CommonUtil.printError(commonVoResp);
            return commonVoResp;
        }
    }


    @PostMapping("/addResourceDoc")
    @ApiOperation(value = "添加文档资源")
    public CommonVoResp doAddResourceDoc(@RequestParam("docPath")MultipartFile file, AddResourceDocVoReq addResourceDocVoReq) {

        CommonVoResp commonVoResp=new CommonVoResp();
        try {


            log.info("老师端上传文档资源参数bean:"+addResourceDocVoReq);
            String path=FileUpdateUtil.fileUploadToBoot(file,FileUpdateUtil.docSuffix);
            AddResourceDocBoReq addResourceDocBoReq=  CommonUtil.objToObj(addResourceDocVoReq, AddResourceDocBoReq.class);

            addResourceDocBoReq.setChapterId(addResourceDocVoReq.getChapterId());//章的编号
            addResourceDocBoReq.setKnowledgePointIdList(Arrays.asList(addResourceDocVoReq.getKnowledgePointIdList().split(",")));//知识点
            addResourceDocBoReq.setResourceTitle(addResourceDocVoReq.getResourceTitle());//资源标题
            addResourceDocBoReq.setDocName(addResourceDocVoReq.getDocName());//文档名称
            addResourceDocBoReq.setDocPath(path);//文档路径
            addResourceDocBoReq.setDocSize(Double.valueOf(addResourceDocVoReq.getDocSize()));//文档大小
            addResourceDocBoReq.setDocType(addResourceDocVoReq.getDocType());//文档类型
            log.info("老师端上传文档资源调用业务接口参数:"+addResourceDocBoReq);
            CommonBoResp commonBoResp= resourceService.addResource(addResourceDocBoReq);
            CommonUtil.printBoResultToVoResult(commonBoResp,commonVoResp);

            log.info("老师端上传文档资源响应bean:"+commonVoResp);
            return commonVoResp;
        }catch (Exception e){
            log.error("教师端添加文档资源出现异常，信息为："+e);
            CommonUtil.printError(commonVoResp);
            return commonVoResp;
        }
    }


    @PostMapping("/addResourceVideo")
    @ApiOperation(value = "添加视频资源")
    public CommonVoResp doAddResourceVideo(@RequestParam("videoFile")MultipartFile file,AddResourceVideoVoReq addResourceVideoVoReq){

        CommonVoResp commonVoResp=new CommonVoResp();
        try {
           log.info("老师端上传视频资源参数bean:"+addResourceVideoVoReq);
            String path=FileUpdateUtil.fileUploadToBoot(file,FileUpdateUtil.videoSuffix);


            AddResourceVideoBoReq addResourceVideoBoReq=  CommonUtil.objToObj(addResourceVideoVoReq, AddResourceVideoBoReq.class);
            addResourceVideoBoReq.setChapterId(addResourceVideoVoReq.getChapterId());//章编号
            addResourceVideoBoReq.setResourceTitle(addResourceVideoVoReq.getResourceTitle());//资源标题
            addResourceVideoBoReq.setKnowledgePointIdList(Arrays.asList(addResourceVideoVoReq.getKnowledgePointIdList().split(",")));//知识点集合
            addResourceVideoBoReq.setVideoPath(path);//文件路径名
            addResourceVideoBoReq.setVideoName(addResourceVideoVoReq.getVideoName());//视频名称
            addResourceVideoBoReq.setVideoSize(addResourceVideoVoReq.getVideoSize());//视频大小
            addResourceVideoBoReq.setVideoDuration(Integer.valueOf(addResourceVideoVoReq.getVideoDuration()));//视频大小
            log.info("老师端上传视频资源调用业务接口参数:"+addResourceVideoBoReq);
            CommonBoResp commonBoResp= resourceService.addResource(addResourceVideoBoReq);
            CommonUtil.printBoResultToVoResult(commonBoResp,commonVoResp);

            log.info("老师端上传视频资源响应bean:"+commonVoResp);
            return commonVoResp;
        }catch (Exception e){
            log.error("教师端添加视频资源出现异常，信息为："+e);
            CommonUtil.printError(commonVoResp);
            return commonVoResp;
        }

    }

    @PostMapping("/addResourceQuestion")
    @ApiOperation(value = "添加试题资源")
    public CommonVoResp doAddResourceQuestion(AddResourceTestVoReq addResourceTestVoReq) {
        CommonVoResp commonVoResp=new CommonVoResp();
        try {
            log.info("教师端添加试题资源参数："+addResourceTestVoReq);

            commonVoResp.setCode(ResultEnum.SUCCESS.getCode());
            commonVoResp.setMsg(ResultEnum.SUCCESS.getMsg());
            for (String str:Arrays.asList(addResourceTestVoReq.getQuestionIds().split(","))) {
                AddResourceTestBoReq addResourceTestBoReq= CommonUtil.objToObj(addResourceTestVoReq,AddResourceTestBoReq.class);
                addResourceTestBoReq.setChapterId(addResourceTestVoReq.getChapterId());
                addResourceTestBoReq.setResourceTitle(addResourceTestVoReq.getResourceTitle());
                addResourceTestBoReq.setKnowledgePointIdList(Arrays.asList(addResourceTestVoReq.getKnowledgePointIdList().split(",")));
                addResourceTestBoReq.setChosenQuestionId(str);
                log.info("教师端添加试题调用业务接口参数："+addResourceTestBoReq);
                CommonBoResp commonBoResp=  resourceService.addResource(addResourceTestBoReq);
                if(commonBoResp.getCode()!=ResultEnum.SUCCESS.getCode()){
                    commonVoResp.setMsg(ResultEnum.NOTSUCCESS.getMsg());
                    commonVoResp.setCode(ResultEnum.NOTSUCCESS.getCode());
                    break;
                }
            }
            log.info("教师端添加试题资源结果："+commonVoResp);
            return commonVoResp;
        }catch (Exception e){
            log.error("教师端添加试题资源出现异常，信息为："+e);
            CommonUtil.printError(commonVoResp);
            return commonVoResp;
        }
    }


    @PostMapping("/queryResource")
    @ApiOperation(value = "根据章得编号查询节得列表信息")
    public ResourceVoResp doQueryResource(ChapterIdVoReq chapterIdVoReq){
        ResourceVoResp resourceVoResp=new ResourceVoResp();
        try {
            log.info("老师端根据章得编号查询节得列表信息参数bean："+chapterIdVoReq);
            ChapterIdBoReq chapterIdBoReq= CommonUtil.objToObj(chapterIdVoReq, ChapterIdBoReq.class);
            ResourceListBoResp resourceListBoResp= resourceService.loadResourceByChapterId(chapterIdBoReq);
            List<ResourceResp> result =new ArrayList<ResourceResp>();
            List<ResourceBoResp> respList=  resourceListBoResp.getResourceBoRespList();

            respList.forEach(resourceBoResp -> {
                ResourceResp resourceResp=new ResourceResp();
                resourceResp.setLearningTime(resourceBoResp.getLearningTime());//学习时长
                resourceResp.setResourceId(resourceBoResp.getResourceId());//资源编号
                resourceResp.setResourceTitle(resourceBoResp.getResourceTitle());//资源标题
                resourceResp.setResourceType(resourceBoResp.getResourceType());//资源类型
                result.add(resourceResp);
            });
            resourceVoResp.setResult(result);
            resourceVoResp.setCode(ResultEnum.SUCCESS.getCode());
            resourceVoResp.setMsg(ResultEnum.SUCCESS.getMsg());
            log.info("老师端获取节得列表信息响应bean:"+resourceVoResp);
            return resourceVoResp;
        }catch (Exception e){
            log.info("老师端获取节得列表信息出现异常，信息为:"+e);
            CommonUtil.printError(resourceVoResp);
            return resourceVoResp;
        }
    }
}
